# -*- coding:utf-8 -*-
from dtrader import dataseries


class TickDataSeries(dataseries.SequenceDataSeries):
    """
    一个包含tick数据结构的队列
    :param max_len:队列的长度，默认为None，1024长度
    :type max_len int.
    """
    def __init__(self, max_len=None):
        super(TickDataSeries, self).__init__(max_len)
        self.__ask_ds = dataseries.SequenceDataSeries(max_len)
        self.__ask_volume_ds = dataseries.SequenceDataSeries(max_len)
        self.__bid_ds = dataseries.SequenceDataSeries(max_len)
        self.__bid_volume_ds = dataseries.SequenceDataSeries(max_len)
        self.__extra_ds = {}

    def __get_or_create_extra_ds(self, name):
        ret = self.__extra_ds.get(name)
        if ret is None:
            ret = dataseries.SequenceDataSeries(self.get_max_len())
            self.extra_ds[name] = ret
        return ret

    def append(self, tick):
        self.append_with_date_time(tick.get_date_time(), tick)

    def append_with_date_time(self, date_time, tick):

        assert(date_time is not None)
        assert(tick is not None)

        super(TickDataSeries, self).append_with_date_time(date_time, tick)
        self.__ask_ds.append_with_date_time(date_time, tick.get_ask())
        self.__ask_volume_ds.append_with_date_time(date_time, tick.get_ask_volume())
        self.__bid_ds.append_with_date_time(date_time, tick.get_bid())
        self.__bid_volume_ds.append_with_date_time(date_time, tick.get_bid_volume())

        # Process extra columns.
        for name, value in tick.get_extra_columns().iteritems():
            extra_ds = self.__get_or_create_extra_ds(name)
            extra_ds.append_with_date_time(date_time, value)

    def get_ask_dataseries(self):
        return self.__ask_ds

    def get_ask_volume_dataseries(self):
        return self.__ask_volume_ds

    def get_bid_dataseries(self):
        return self.__bid_ds

    def get_bid_volume_dataseries(self):
        return self.__bid_volume_ds

    def get_extra_dataseries(self, name):
        return self.__get_or_create_extra_ds(name)
